package com.vichat.common.rudp.thread;

import com.vichat.common.rudp.RudpDataUtil;
import com.vichat.common.rudp.RudpTransData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;


public class RudpReceiveDataManager extends Thread {
    private static transient Logger logger = LoggerFactory.getLogger(RudpReceiveDataManager.class);

    public void run() {
        //每隔10秒回收过期数据
        while (true) {
            try {
                HashMap cloneTransData = RudpDataUtil.getTransCloneData();
                long curTime = System.currentTimeMillis();
                for (Iterator<RudpTransData> iter = cloneTransData.values().iterator(); iter.hasNext(); ) {
                    try {
                        RudpTransData transdata = iter.next();
                        //TransLocker transCreTime = transdata.createTime;
                        if (curTime - transdata.createTime >= 300000) {
                            if (transdata.blockNum > 1 && transdata.blockNum != transdata.receiveNum) {
                                logger.debug("多包状态" + transdata.blockNum + "," + transdata.receiveNum);
                            }
                            RudpDataUtil.removeTransData(transdata.getStringTrans());

                            logger.debug("成功移除过期接受数据" + transdata.getStringTrans());
                        }
                    } catch (Exception e) {
                        logger.debug("移除过期接收数据出错", e);
                    }
                }
                if (!RudpDataUtil.transDataCont.isEmpty()) {
                    logger.debug(",数据对象" + RudpDataUtil.transDataCont.size());
                }
                this.sleep(10000);
            } catch (Exception e) {
                logger.debug("处理接受接收数据出错", e);
            }

        }

    }

}
